import taobao_open

from libs.common import success, error
from libs.form import RawJsonForm

app_key = '33328606'
app_secret = '4222872203745d62413bdb05e7519dc7'


def dg_optimus_material(request):
    form = RawJsonForm(request.body)
    member_id = form.get('member_id')
    req = taobao_open.api.TbkDgOptimusMaterialRequest()
    req.set_app_info(taobao_open.appinfo(app_key, app_secret))
    req.special_id = member_id
    req.adzone_id = 111858100437
    req.material_id = 27160
    result = {}
    try:
        resp = req.getResponse()
        result = resp.get('tbk_dg_optimus_material_response')
    except Exception as e:
        print(e)
    return success(**result)


def dg_material_optional(request):
    form = RawJsonForm(request.body)
    keyword = form.get('q')
    if keyword == '':
        return error('无效的商品查询词')
    member_id = form.get('member_id')
    req = taobao_open.api.TbkDgMaterialOptionalRequest()
    req.set_app_info(taobao_open.appinfo(app_key, app_secret))
    req.special_id = member_id
    req.adzone_id = 111858100437
    req.q = keyword
    result = {}
    try:
        resp = req.getResponse()
        result = resp.get('tbk_dg_material_optional_response')
    except Exception as e:
        print(e)
    return success(**result)


def sc_publisher_info_get(request):
    form = RawJsonForm(request.body)
    session = form.get('session')
    if session == '':
        return error('无效的授权信息')
    req = taobao_open.api.TbkScPublisherInfoGetRequest()
    req.set_app_info(taobao_open.appinfo(app_key, app_secret))
    req.info_type = 2
    req.relation_app = 'common'
    result = {}
    try:
        resp = req.getResponse(session)
        result = resp.get('tbk_sc_publisher_info_get_response')
    except Exception as e:
        print(e)
    return success(**result)


def sc_publisher_info_save(request):
    form = RawJsonForm(request.body)
    session = form.get('session')
    if session == '':
        return error('无效的授权信息')
    req = taobao_open.api.TbkScPublisherInfoSaveRequest()
    req.set_app_info(taobao_open.appinfo(app_key, app_secret))
    req.inviter_code = 'SJ5FGM'
    req.info_type = 1
    result = {}
    try:
        resp = req.getResponse(session)
        result = resp.get('tbk_sc_publisher_info_save_response')
    except Exception as e:
        print(e)
    return success(**result)
